﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using OAuth.Dapper;
using OAuth.Dapper.Models;
using Dapper;
using Dapper.Contrib.Extensions;
using Microsoft.AspNetCore.Mvc;
using OAuth.Common.Dapper;
using OAuth.Server.Models.Account;

namespace OAuth.Server.Domain
{
    public class AccountRepository : IAccountRepository
    {
        public async Task<List<Account>> GetAccounts()
        {
            using (var conn = DBConfig.GetSqlConnection())
            {
                var result = await conn.QueryAsync<Account>("SELECT * FROM account").ConfigureAwait(false);
                return result.ToList();
            }
        }



        /// <summary>
        /// 其他扩展
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Account> GetAccountById(int id)
        {
            using (var conn = DBConfig.GetSqlConnection())
            {
                var result = await conn.GetAsync<Account>(id).ConfigureAwait(false);
                return result;
            }
        }
        /// <summary>
        /// 事务
        /// </summary>
        /// <returns></returns>

        public async Task Create(Account model)
        {
            using (var conn = DBConfig.GetSqlConnection())
            {
                await conn.ExecuteAsync("insert account into () values()", model);


                using (var transaction = conn.BeginTransaction())
                {
                    conn.Execute("sql", new { CustomerName = "Mark" }, transaction);
                    conn.Execute("sql", new { CustomerName = "Mark" }, transaction);
                    transaction.Commit();
                }
            }
        }

        public Task<int> Insert(Account model)
        {
            throw new NotImplementedException();
        }

        public Task<Account> Login(string userName, string pwassword)
        {
            throw new NotImplementedException();
        }

        public Task<Account> GetAccountByUserNameAsync(string userName)
        {
            throw new NotImplementedException();
        }
    }
}
